package chances.epg.ui.taglib.html;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.StringUtils;

import chances.epg.ui.taglib.layout.BaseLayoutTag;

@SuppressWarnings("serial")
public class BodyTag extends BaseLayoutTag {
	private String focusId;
	private String onload;
	private String onunload;
	private String onplay;
	private String backUrl;
	private String defaultFocusId;
	
	private static final String SUBJECT_PAGE_REGEX = "det/subject/.*/subject/subjectpage_*";
	private static final String POSITION_PAGE_REGEX = "position/BizPosition_.*/item/subject/*";

	@Override
	public void doAfterStart() throws JspException {
		String afocusId = this.focusId;
		if (StringUtils.isEmpty(afocusId)) {
			afocusId = epgPageContext.getFocusId();
		}
		if (StringUtils.isEmpty(afocusId)) {
			afocusId = defaultFocusId;
		}

		epgPageContext.setFocusId(afocusId);

		if (StringUtils.isNotEmpty(this.getOnkeypress())) {
			epgPageContext.setKeyHandler(this.getOnkeypress());
		}

		if (StringUtils.isNotEmpty(this.onplay)) {
			epgPageContext.setPlayHandler(this.onplay);

		}
		if (this.rect == null) {
			this.setRectStyle(epgPageContext.getViewport().getStyle());
		} else {
			epgPageContext.setViewport(this.getRect());
		}
		if (this.backUrl != null && !isExcludeBackUrl()) {
			epgPageContext.setBackUrl(this.backUrl);
		}
		super.doAfterStart();
	}

	/**
	 * 如果是专题详情页过来的，还是返回到专题详情页，而不是前一个列表页或展示栏目
	 * 
	 * @return boolean
	 */
	private boolean isExcludeBackUrl() {
		String epgPageBackUrl = this.epgPageContext.getBackUrl();
		if("null".equals(epgPageBackUrl) || StringUtils.isEmpty(epgPageBackUrl)) {
			return false;
		}
		if(isFind(SUBJECT_PAGE_REGEX, epgPageBackUrl) || isFind(POSITION_PAGE_REGEX, epgPageBackUrl)) {
			return true;
		}
		return false;
	}
	
	private boolean isFind(String regEx, String str) {
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(str);
		boolean rs = matcher.find();
		return rs;
	}

	@Override
	public int doStartTag() throws JspException {
		super.id = "body";
		return super.doStartTag();
	}

	@Override
	public String getVm() {
		return "body";
	}

	public String getOnload() {
		return onload;
	}

	public void setOnload(String onload) {
		this.onload = onload;
	}

	public String getOnunload() {
		return onunload;
	}

	public void setOnunload(String onunload) {
		this.onunload = onunload;
	}

	public String getFocusId() {
		return focusId;
	}

	public void setFocusId(String focusId) {
		this.focusId = focusId;
	}

	public String getBackUrl() {
		return backUrl;
	}

	public void setBackUrl(String backUrl) {
		this.backUrl = backUrl;
	}

	public String getDefaultFocusId() {
		return defaultFocusId;
	}

	public void setDefaultFocusId(String defaultFocusId) {
		this.defaultFocusId = defaultFocusId;
	}

	public String getOnplay() {
		return onplay;
	}

	public void setOnplay(String onplay) {
		this.onplay = onplay;
	}

}
